#### Cleaning the Working Environment ####
rm(list = ls(all=T))
gc()

#### Getting the Working Directory ####
getwd()

#### Creating Empty Folder for Tables in the Working Directory ####
dir.create("Tables")

#### Loading Packages ####
library(haven)
library(sjPlot)
library(nnet)
library(stargazer)
library(tidyverse)
library(viridisLite)
library(viridis)
library(forcats)
library(data.table)
library(openxlsx)
library(psych)
library(lme4)
library(lattice)
library(ggeffects)
library(sjlabelled)
library(psych)
library(naniar)
library(dotwhisker)
library(broom)
library(lattice)
library(RColorBrewer)
library(merTools)
library(sjPlot)
library(ggplot2)
library(openxlsx)
library(apaTables)
library(readxl)
library(ggrepel)

#### Inserting functions ####

qcut2 <- function(x, n) {
  findInterval(x, quantile(x, seq(0, 1, length = n + 1), na.rm=T), all.inside = T)
}


#### Loading DATASETS ####
issp_2007 <- read_dta("DATASETS/ZA4850_v2-0-0.dta")
issp_2007$V5 <- as.numeric(issp_2007$V5)

#### Checking and Preparing Sleep Variables in the DATASETS ####
table(issp_2007$V67, exclude = NULL)
table(issp_2007$V68, exclude = NULL)

issp_2007$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007$V68), format='%H%M'), 12, 16)
issp_2007$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007$V67), format='%H%M'), 12, 16)

table(issp_2007$sleeping_time_hour_alternative, exclude = NULL)
table(issp_2007$getting_up_time_hour_alternative, exclude = NULL)

issp_2007$Sleeping_Hour <- format(as.POSIXct(issp_2007$sleeping_time_hour_alternative, format="%H:%M"),"%H")
issp_2007$Sleeping_Minute <- format(as.POSIXct(issp_2007$sleeping_time_hour_alternative, format="%H:%M"),"%M")

issp_2007$Waking_Hour <- format(as.POSIXct(issp_2007$getting_up_time_hour_alternative, format="%H:%M"),"%H")
issp_2007$Waking_Minute <- format(as.POSIXct(issp_2007$getting_up_time_hour_alternative, format="%H:%M"),"%M")

issp_2007$sleeping_time_hour_alternative <- as.numeric(issp_2007$Sleeping_Hour) + as.numeric(issp_2007$Sleeping_Minute)/60
issp_2007$getting_up_time_hour_alternative <- as.numeric(issp_2007$Waking_Hour) + as.numeric(issp_2007$Waking_Minute)/60

#### Loading Netherlands Dataset ####
issp_2007_netherlands <- read_dta("DATASETS/issp_2007nl_def.dta")

issp_2007_netherlands$nl_hinc <- ifelse(issp_2007_netherlands$nl_hinc==99999, NA, issp_2007_netherlands$nl_hinc)
issp_2007_netherlands$NL_INC <- issp_2007_netherlands$nl_hinc
table(issp_2007_netherlands$NL_INC, exclude = NULL)
str(issp_2007_netherlands$NL_INC)
issp_2007_netherlands$NL_INC <- as.numeric(issp_2007_netherlands$NL_INC)
table(qcut2(issp_2007_netherlands$NL_INC, 5), exclude = NULL)
issp_2007_netherlands$NL_INC_rank <- qcut2(issp_2007_netherlands$NL_INC, 5)
table(issp_2007_netherlands$NL_INC_rank, exclude = NULL)

issp_2007_netherlands$V58 <- ifelse(issp_2007_netherlands$V58==-1 | issp_2007_netherlands$V58==8, NA, issp_2007_netherlands$V58)
table(issp_2007_netherlands$V58, exclude = NULL)

issp_2007_netherlands$V66 <- ifelse(issp_2007_netherlands$V66==-1, NA, issp_2007_netherlands$V66)
table(issp_2007_netherlands$V66, exclude = NULL)

issp_2007_netherlands$V67 <- ifelse(issp_2007_netherlands$V67==-1, NA, issp_2007_netherlands$V67)
table(issp_2007_netherlands$V67, exclude = NULL)

issp_2007_netherlands$V68 <- ifelse(issp_2007_netherlands$V68==-1, NA, issp_2007_netherlands$V68)
table(issp_2007_netherlands$V68, exclude = NULL)

issp_2007_netherlands$degree <- ifelse(issp_2007_netherlands$degree==9, NA, issp_2007_netherlands$degree)
table(issp_2007_netherlands$degree, exclude = NULL)

issp_2007_netherlands$nl_hinc <- ifelse(issp_2007_netherlands$nl_hinc==99999, NA, issp_2007_netherlands$nl_hinc)
table(issp_2007_netherlands$nl_hinc, exclude = NULL)

issp_2007_netherlands$PARTY_LR <- ifelse(issp_2007_netherlands$PARTY_LR==7 | issp_2007_netherlands$PARTY_LR==9, NA, issp_2007_netherlands$PARTY_LR)
table(issp_2007_netherlands$PARTY_LR, exclude = NULL)

issp_2007_netherlands$attend <- ifelse(issp_2007_netherlands$attend==98 | issp_2007_netherlands$attend==99, NA, issp_2007_netherlands$attend)
table(issp_2007_netherlands$attend, exclude = NULL)

table(issp_2007_netherlands$relig, exclude = NULL)
issp_2007_netherlands$relig <- ifelse(issp_2007_netherlands$relig==0, NA, issp_2007_netherlands$relig)

table(issp_2007_netherlands$religgrp, exclude = NULL)
issp_2007_netherlands$religgrp <- ifelse(issp_2007_netherlands$religgrp==99, NA, issp_2007_netherlands$religgrp)

#### Checking and Preparing Sleep Variables in the Netherlands Dataset ####

issp_2007_netherlands$V67 <- as.numeric(issp_2007_netherlands$V67)
table(issp_2007_netherlands$V67, exclude = NULL)
issp_2007_netherlands$V67 <- issp_2007_netherlands$V67*100

issp_2007_netherlands$V68 <- as.numeric(issp_2007_netherlands$V68)
table(issp_2007_netherlands$V68, exclude = NULL)
issp_2007_netherlands$V68 <- issp_2007_netherlands$V68*100

issp_2007_netherlands$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007_netherlands$V68), format='%H%M'), 12, 16)
issp_2007_netherlands$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", issp_2007_netherlands$V67), format='%H%M'), 12, 16)

table(issp_2007_netherlands$sleeping_time_hour_alternative, exclude = NULL)
table(issp_2007_netherlands$getting_up_time_hour_alternative, exclude = NULL)

issp_2007_netherlands$Sleeping_Hour <- format(as.POSIXct(issp_2007_netherlands$sleeping_time_hour_alternative, format="%H:%M"),"%H")
issp_2007_netherlands$Sleeping_Minute <- format(as.POSIXct(issp_2007_netherlands$sleeping_time_hour_alternative, format="%H:%M"),"%M")

issp_2007_netherlands$Waking_Hour <- format(as.POSIXct(issp_2007_netherlands$getting_up_time_hour_alternative, format="%H:%M"),"%H")
issp_2007_netherlands$Waking_Minute <- format(as.POSIXct(issp_2007_netherlands$getting_up_time_hour_alternative, format="%H:%M"),"%M")

issp_2007_netherlands$sleeping_time_hour_alternative <- as.numeric(issp_2007_netherlands$Sleeping_Hour) + as.numeric(issp_2007_netherlands$Sleeping_Minute)/60
issp_2007_netherlands$getting_up_time_hour_alternative <- as.numeric(issp_2007_netherlands$Waking_Hour) + as.numeric(issp_2007_netherlands$Waking_Minute)/60

#### Merging ISSP 2007 and the Netherlands Datasets ####
issp_2007 <- dplyr::bind_rows(issp_2007, issp_2007_netherlands)

table(issp_2007$religgrp, exclude = NULL)

issp_2007$religious_denomination <- NA
issp_2007$religious_denomination[issp_2007$religgrp==1] <- "No_religion"
issp_2007$religious_denomination[issp_2007$religgrp==2] <- "Roman_Catholic"
issp_2007$religious_denomination[issp_2007$religgrp==3] <- "Protestant"
issp_2007$religious_denomination[issp_2007$religgrp==4] <- "Christian_Orthodox"
issp_2007$religious_denomination[issp_2007$religgrp==5] <- "Jewish"
issp_2007$religious_denomination[issp_2007$religgrp==6] <- "Islam"
issp_2007$religious_denomination[issp_2007$religgrp==7] <- "Buddhism"
issp_2007$religious_denomination[issp_2007$religgrp==8] <- "Hinduism"
issp_2007$religious_denomination[issp_2007$religgrp==9] <- "Other_Christian_Religions"
issp_2007$religious_denomination[issp_2007$religgrp==10] <- "Other_Eastern_Religions"
issp_2007$religious_denomination[issp_2007$religgrp==11] <- "Other_Religions"
issp_2007$religious_denomination <- ifelse(is.na(issp_2007$religious_denomination), "Missing", issp_2007$religious_denomination)
table(issp_2007$religious_denomination, exclude = NULL)

#### Dropping Observations with Missing Sleep Variables ####
issp_2007 <- subset(issp_2007, subset = (!is.na(issp_2007$V67) | !is.na(issp_2007$V68)))

#### Calculating Duration of Sleep ####
issp_2007$sleep_duration_alternative <-  (issp_2007$getting_up_time_hour_alternative-issp_2007$sleeping_time_hour_alternative) 
table(issp_2007$sleep_duration_alternative, exclude = NULL)
issp_2007$sleep_duration_alternative <-  ifelse(issp_2007$getting_up_time_hour_alternative>=issp_2007$sleeping_time_hour_alternative, issp_2007$sleep_duration_alternative, (24 - issp_2007$sleeping_time_hour_alternative) + issp_2007$getting_up_time_hour_alternative) 
table(issp_2007$sleep_duration_alternative, exclude = NULL)

#### Calculating Chronotype ####
issp_2007$mid_point_alternative <- issp_2007$getting_up_time_hour_alternative - issp_2007$sleep_duration_alternative/2
issp_2007$mid_point_alternative <- ifelse(issp_2007$mid_point_alternative<0, issp_2007$mid_point_alternative+24, issp_2007$mid_point_alternative)
table(issp_2007$mid_point_alternative)

#### Exclusion Criteria based on Duration of Sleep and Chronotype ####
#issp_2007 <- subset(issp_2007, subset = (issp_2007$sleeping_time_hour_alternative<=6 | issp_2007$sleeping_time_hour_alternative>=19) & (issp_2007$getting_up_time_hour_alternative<=15 & issp_2007$getting_up_time_hour_alternative>=4))
#issp_2007 <- subset(issp_2007, subset = issp_2007$sleep_duration_alternative>=2 & sleep_duration_alternative<=18)

#### Reverse Coding Chronotype - Bigger Values for Morningness and Smaller Values for Eveningness ####
table(issp_2007$mid_point_alternative, exclude = NULL)
issp_2007$ORIGINAL_MID_POINT <- issp_2007$mid_point_alternative
issp_2007$midcenter <- issp_2007$mid_point_alternative-16
table(issp_2007$midcenter, exclude = NULL)
issp_2007$cond3 <- issp_2007$midcenter + 24
issp_2007$midcenter <- ifelse(issp_2007$midcenter<0, issp_2007$cond3, issp_2007$midcenter)
table(issp_2007$midcenter, exclude = NULL)
issp_2007$mid_point_alternative <- issp_2007$midcenter
table(issp_2007$mid_point_alternative, exclude = NULL)
issp_2007$chronotype_interval <-  issp_2007$mid_point_alternative*-1
issp_2007$sleep_duration <-  issp_2007$sleep_duration_alternative
table(issp_2007$chronotype_interval, exclude = NULL)
table(issp_2007$sleep_duration, exclude = NULL)

#### Checking and Prepating Covariates ####
table(issp_2007$degree, exclude = NULL)
issp_2007$degree <- ifelse(issp_2007$degree>5, NA, issp_2007$degree)

table(issp_2007$sex, exclude = NULL) # 1 - male, 2 - female
issp_2007$sex <- issp_2007$sex
issp_2007$sex[issp_2007$sex==1] <- 1
issp_2007$sex[issp_2007$sex==2] <- 0
table(issp_2007$sex, exclude = NULL)


table(issp_2007$V66, exclude = NULL)
issp_2007$day_off <- NA
issp_2007$day_off[issp_2007$V66==2] <- 1 ## Weekend
issp_2007$day_off[issp_2007$V66==1] <- 0 ## Weekday
table(issp_2007$day_off, exclude = NULL)


table(issp_2007$V58, exclude = NULL)
issp_2007$political_interest <- issp_2007$V58
table(issp_2007$political_interest, exclude = NULL)
issp_2007$political_interest <- ifelse(issp_2007$political_interest>4, NA, issp_2007$political_interest)
issp_2007$political_interest <- 5 - issp_2007$political_interest

table(issp_2007$urbrural, exclude = NULL)
issp_2007$urban_rural_area <- NA
issp_2007$urban_rural_area[issp_2007$urbrural==5 | issp_2007$urbrural==4] <- 1 # to match with Greece data
issp_2007$urban_rural_area[issp_2007$urbrural==3] <- 2
issp_2007$urban_rural_area[issp_2007$urbrural==2] <- 3
issp_2007$urban_rural_area[issp_2007$urbrural==1] <- 4
table(issp_2007$urban_rural_area, exclude = NULL)

table(issp_2007$attend, exclude = NULL)

issp_2007$religiosity <- NA
issp_2007$religiosity[issp_2007$attend==8] <- 1
issp_2007$religiosity[issp_2007$attend==7 | issp_2007$attend==6] <- 2
issp_2007$religiosity[issp_2007$attend==5] <- 3
issp_2007$religiosity[issp_2007$attend==4] <- 4
issp_2007$religiosity[issp_2007$attend==3] <- 5
issp_2007$religiosity[issp_2007$attend==1 | issp_2007$attend==2] <- 6
table(issp_2007$religiosity, exclude = NULL)

issp_2007$age <- ifelse(issp_2007$age<18, NA, issp_2007$age)

issp_2007$PARTY_LR <- ifelse(issp_2007$PARTY_LR>5, NA, issp_2007$PARTY_LR)
table(issp_2007$PARTY_LR, exclude = NULL)

#### Loading Greece Dataset ####

greece_data <- read_dta("DATASETS/Greece_2020.dta")

greece_data$V67 <-  greece_data$hourtime1 + greece_data$minutetime1/100
table(greece_data$V67, exclude = NULL)
greece_data$V67 <- greece_data$V67*100

greece_data$V68 <-  greece_data$hourtime2_corrected + greece_data$minutetime2/100
table(greece_data$V68, exclude = NULL)
greece_data$V68 <- greece_data$V68*100

greece_data$sleeping_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", greece_data$V68), format='%H%M'), 12, 16)
greece_data$getting_up_time_hour_alternative <- substr(as.POSIXct(sprintf("%04.0f", greece_data$V67), format='%H%M'), 12, 16)

table(greece_data$sleeping_time_hour_alternative, exclude = NULL)
table(greece_data$getting_up_time_hour_alternative, exclude = NULL)

greece_data$Sleeping_Hour <- format(as.POSIXct(greece_data$sleeping_time_hour_alternative, format="%H:%M"),"%H")
greece_data$Sleeping_Minute <- format(as.POSIXct(greece_data$sleeping_time_hour_alternative, format="%H:%M"),"%M")

greece_data$Waking_Hour <- format(as.POSIXct(greece_data$getting_up_time_hour_alternative, format="%H:%M"),"%H")
greece_data$Waking_Minute <- format(as.POSIXct(greece_data$getting_up_time_hour_alternative, format="%H:%M"),"%M")

greece_data$sleeping_time_hour_alternative <- as.numeric(greece_data$Sleeping_Hour) + as.numeric(greece_data$Sleeping_Minute)/60
greece_data$getting_up_time_hour_alternative <- as.numeric(greece_data$Waking_Hour) + as.numeric(greece_data$Waking_Minute)/60

greece_data$sleep_duration_alternative <-  (greece_data$getting_up_time_hour_alternative-greece_data$sleeping_time_hour_alternative) 
table(greece_data$sleep_duration_alternative, exclude = NULL)
greece_data$sleep_duration_alternative <-  ifelse(greece_data$getting_up_time_hour_alternative>=greece_data$sleeping_time_hour_alternative, greece_data$sleep_duration_alternative, (24 - greece_data$sleeping_time_hour_alternative) + greece_data$getting_up_time_hour_alternative) 
table(greece_data$sleep_duration_alternative, exclude = NULL)

greece_data$mid_point_alternative <- greece_data$getting_up_time_hour_alternative - greece_data$sleep_duration_alternative/2
greece_data$mid_point_alternative <- ifelse(greece_data$mid_point_alternative<0, greece_data$mid_point_alternative+24, greece_data$mid_point_alternative)
table(greece_data$mid_point_alternative)

#greece_data <- subset(greece_data, subset = (greece_data$sleeping_time_hour_alternative<=6 | greece_data$sleeping_time_hour_alternative>=19) & (greece_data$getting_up_time_hour_alternative<=15 & greece_data$getting_up_time_hour_alternative>=4))
#greece_data <- subset(greece_data, subset = greece_data$sleep_duration_alternative>=2 & sleep_duration_alternative<=18)

table(greece_data$mid_point_alternative, exclude = NULL)
greece_data$ORIGINAL_MID_POINT <- greece_data$mid_point_alternative
greece_data$midcenter <- greece_data$mid_point_alternative-16
table(greece_data$midcenter, exclude = NULL)
greece_data$cond3 <- greece_data$midcenter + 24
greece_data$midcenter <- ifelse(greece_data$midcenter<0, greece_data$cond3, greece_data$midcenter)
table(greece_data$midcenter, exclude = NULL)
greece_data$mid_point_alternative <- greece_data$midcenter
table(greece_data$mid_point_alternative, exclude = NULL)
greece_data$chronotype_interval <-  greece_data$mid_point_alternative*-1
greece_data$sleep_duration <-  greece_data$sleep_duration_alternative
table(greece_data$chronotype_interval, exclude = NULL)
table(greece_data$sleep_duration, exclude = NULL)

greece_data$degree <- NA
greece_data$degree[greece_data$D03f==1] <- 0
greece_data$degree[greece_data$D03f==2 | greece_data$D03f==3] <- 1
greece_data$degree[greece_data$D03f==4 | greece_data$D03f==5] <- 2
greece_data$degree[greece_data$D03f==6] <- 3
greece_data$degree[greece_data$D03f==7] <- 4
greece_data$degree[greece_data$D03f==8 | greece_data$D03f==9 | greece_data$D03f==10] <- 5
table(greece_data$degree, exclude = NULL)

greece_data$sex <- NA
greece_data$sex[greece_data$D02f==1] <- 1
greece_data$sex[greece_data$D02f==2] <- 0
table(greece_data$sex, exclude = NULL)

greece_data$interview_time <- paste(greece_data$qyear, greece_data$qmonth, greece_data$qday, sep = "-")
table(greece_data$interview_time, exclude = NULL)

greece_data$day_off <- NA
greece_data$day_off[greece_data$interview_time=="2019-12-14" |
                      greece_data$interview_time=="2019-12-15" |
                      greece_data$interview_time== "2019-12-22" |
                      greece_data$interview_time=="2019-12-28" |
                      greece_data$interview_time=="2019-12-29" |
                      greece_data$interview_time== "2020-1-4" |
                      greece_data$interview_time=="2020-1-5" |
                      greece_data$interview_time== "2020-2-1" |
                      greece_data$interview_time== "2020-2-2" |
                      greece_data$interview_time=="2020-2-8" |
                      greece_data$interview_time=="2020-2-9" |
                      greece_data$interview_time=="2020-2-15" |
                      greece_data$interview_time== "2020-2-16"] <- 1
greece_data$day_off <- ifelse(is.na(greece_data$day_off), 0, greece_data$day_off)
table(greece_data$day_off, exclude = NULL)

table(greece_data$Q01f, exclude = NULL)
greece_data$political_interest <- greece_data$Q01f
table(greece_data$political_interest, exclude = NULL)
greece_data$political_interest <- ifelse(greece_data$political_interest>4, NA, greece_data$political_interest)
greece_data$political_interest <- 5 - greece_data$political_interest

greece_data$urban_rural_area <- greece_data$D19f
greece_data$urban_rural_area <- ifelse(greece_data$urban_rural_area==5, NA, greece_data$urban_rural_area)
table(greece_data$urban_rural_area, exclude = NULL)

table(greece_data$D11f, exclude = NULL)
greece_data$religiosity <- greece_data$D11f
greece_data$religiosity <- ifelse(greece_data$religiosity==7, NA, greece_data$religiosity)
table(greece_data$religiosity, exclude = NULL)

table(greece_data$Q18f, exclude = NULL)
greece_data$ideology <- greece_data$Q18f
greece_data$ideology <- ifelse(greece_data$ideology==12, NA, greece_data$ideology)
table(greece_data$ideology, exclude = NULL)
greece_data$PARTY_LR <- (5-1)/(11-1)*(greece_data$ideology-11) + 5
table(greece_data$PARTY_LR, exclude = NULL)
greece_data$PARTY_LR <- round(greece_data$PARTY_LR, 0)
greece_data$PARTY_LR[greece_data$ideology==5] <- 2
greece_data$PARTY_LR[greece_data$ideology==7] <- 4
table(greece_data$PARTY_LR, greece_data$ideology, exclude = NULL)
table(greece_data$PARTY_LR, exclude = NULL)

table(greece_data$age, exclude = NULL)
greece_data$age <- ifelse(greece_data$age<18, NA, greece_data$age)
greece_data <- subset(greece_data, subset = !is.na(age))

table(greece_data$D09f, exclude = NULL)
str(greece_data$D09f)
greece_data$D09f <- as.numeric(greece_data$D09f)
greece_data$GR_INC_rank <- greece_data$D09f
greece_data$GR_INC_rank <- ifelse(greece_data$GR_INC_rank==6, NA, greece_data$GR_INC_rank)
table(greece_data$GR_INC_rank, exclude = NULL)

greece_data$V5 <- 2020

issp_2007 <- dplyr::bind_rows(issp_2007, greece_data)

#### Income variable for all ####

table(issp_2007$FI_INC, exclude = NULL)
str(issp_2007$FI_INC)
issp_2007$FI_INC <- as.numeric(issp_2007$FI_INC)
table(qcut2(issp_2007$FI_INC, 5), exclude = NULL)
issp_2007$FI_INC_rank <- qcut2(issp_2007$FI_INC, 5)
table(issp_2007$FI_INC_rank, exclude = NULL)

table(issp_2007$IE_INC, exclude = NULL)
str(issp_2007$IE_INC)
issp_2007$IE_INC <- as.numeric(issp_2007$IE_INC)
table(qcut2(issp_2007$IE_INC, 5), exclude = NULL)
issp_2007$IE_INC_rank <- qcut2(issp_2007$IE_INC, 5)
table(issp_2007$IE_INC_rank, exclude = NULL)

table(issp_2007$KR_INC, exclude = NULL)
str(issp_2007$KR_INC)
issp_2007$KR_INC <- as.numeric(issp_2007$KR_INC)
table(qcut2(issp_2007$KR_INC, 5), exclude = NULL)
issp_2007$KR_INC_rank <- qcut2(issp_2007$KR_INC, 5)
table(issp_2007$KR_INC_rank, exclude = NULL)

table(issp_2007$MX_INC, exclude = NULL)
str(issp_2007$MX_INC)
issp_2007$MX_INC <- as.numeric(issp_2007$MX_INC)
table(qcut2(issp_2007$MX_INC, 5), exclude = NULL)
issp_2007$MX_INC_rank <- qcut2(issp_2007$MX_INC, 5)
table(issp_2007$MX_INC_rank, exclude = NULL)

table(issp_2007$PH_INC, exclude = NULL)
str(issp_2007$PH_INC)
issp_2007$PH_INC <- as.numeric(issp_2007$PH_INC)
table(qcut2(issp_2007$PH_INC, 5), exclude = NULL)
issp_2007$PH_INC_rank <- qcut2(issp_2007$PH_INC, 5)
table(issp_2007$PH_INC_rank, exclude = NULL)

table(issp_2007$RU_INC, exclude = NULL)
str(issp_2007$RU_INC)
issp_2007$RU_INC <- as.numeric(issp_2007$RU_INC)
table(qcut2(issp_2007$RU_INC, 5), exclude = NULL)
issp_2007$RU_INC_rank <- qcut2(issp_2007$RU_INC, 5)
table(issp_2007$RU_INC_rank, exclude = NULL)

table(issp_2007$CH_INC, exclude = NULL)
str(issp_2007$CH_INC)
issp_2007$CH_INC <- as.numeric(issp_2007$CH_INC)
table(qcut2(issp_2007$CH_INC, 5), exclude = NULL)
issp_2007$CH_INC_rank <- qcut2(issp_2007$CH_INC, 5)
table(issp_2007$CH_INC_rank, exclude = NULL)

table(issp_2007$NZ_INC, exclude = NULL)
str(issp_2007$NZ_INC)
issp_2007$NZ_INC <- as.numeric(issp_2007$NZ_INC)
table(qcut2(issp_2007$NZ_INC, 5), exclude = NULL)
issp_2007$NZ_INC_rank <- qcut2(issp_2007$NZ_INC, 5)
table(issp_2007$NZ_INC_rank, exclude = NULL)

issp_2007$income <- rowSums(issp_2007[,c("FI_INC_rank", 
                                         "IE_INC_rank",
                                         "KR_INC_rank",
                                         "MX_INC_rank",
                                         "NL_INC_rank",
                                         "NZ_INC_rank",
                                         "PH_INC_rank",
                                         "RU_INC_rank",
                                         "CH_INC_rank",
                                         "GR_INC_rank")], na.rm = T)
table(issp_2007$income, exclude = NULL)
table(issp_2007$income, issp_2007$FI_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$IE_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$KR_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$MX_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$NL_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$NZ_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$PH_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$RU_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$CH_INC_rank, exclude = NULL)
table(issp_2007$income, issp_2007$GR_INC_rank, exclude = NULL)
issp_2007$income <- ifelse(issp_2007$income==0, NA, issp_2007$income)
table(issp_2007$income, exclude = NULL)

table(issp_2007$V5, exclude = NULL)
issp_2007$country <- NA
issp_2007$country[issp_2007$V5==246] <- 'Finland'
issp_2007$country[issp_2007$V5==2020] <- 'Greece'
issp_2007$country[issp_2007$V5==372] <- 'Ireland'
issp_2007$country[issp_2007$V5==410] <- 'South Korea'
issp_2007$country[issp_2007$V5==484] <- 'Mexico'
issp_2007$country[issp_2007$V5==528] <- 'the Netherlands'
issp_2007$country[issp_2007$V5==554] <- 'New Zealand'
issp_2007$country[issp_2007$V5==608] <- 'the Philippines'
issp_2007$country[issp_2007$V5==643] <- 'Russia'
issp_2007$country[issp_2007$V5==756] <- 'Switzerland'
table(issp_2007$country, exclude = NULL)
issp_2007$country <- factor(issp_2007$country, 
                            levels = c('Finland','Greece', 'Ireland', 'Mexico', 'the Netherlands', 'New Zealand', 'the Philippines', 'Russia', 'South Korea', 'Switzerland'))
table(issp_2007$country, exclude = NULL)


issp_2007 <- subset(issp_2007, subset = country == 'Finland' | 
                      country == 'Greece' |
                      country == 'Ireland' | 
                      country == 'South Korea' | 
                      country == 'Mexico' | 
                      country == 'the Netherlands' | 
                      country == 'New Zealand' | 
                      country == 'the Philippines' | country == 'Russia' | country == 'Switzerland')

#### Getting Country-Region Level Variables ####
table(issp_2007$IE_REG, exclude = NULL)
table(issp_2007$FI_REG, exclude = NULL)
table(issp_2007$KR_REG, exclude = NULL)
table(issp_2007$MX_REG, exclude = NULL)
table(issp_2007$NL_REG, exclude = NULL)
table(issp_2007$NZ_REG, exclude = NULL)
table(issp_2007$PH_REG, exclude = NULL)
table(issp_2007$RU_REG, exclude = NULL)
table(issp_2007$CH_REG, exclude = NULL)

issp_2007$region_id <- NA
issp_2007$region_id <- rowSums( issp_2007[,c("IE_REG", "FI_REG", "KR_REG",
                                             "MX_REG", "NL_REG", "NZ_REG",
                                             "PH_REG", "RU_REG", "CH_REG")], na.rm = T)

table(issp_2007$region_id, exclude = NULL)

issp_2007$region_id <- ifelse(issp_2007$region_id==0, NA, issp_2007$region_id)

issp_2007$region_id_merging <- paste(issp_2007$V5, issp_2007$region_id)
table(issp_2007$region_id_merging, exclude = NULL)  

geocodes_temperatures <- read_excel("Geographic Dataset/geocodes_temperatures.xlsx")
geocodes_temperatures$region_id_merging <- paste(geocodes_temperatures$V5, geocodes_temperatures$region_id)
table(geocodes_temperatures$region_id_merging, exclude = NULL) 

issp_2007 <- merge(issp_2007,geocodes_temperatures,by="region_id_merging", all = T)

#### Pooled Data - Grand and group mean centering ####

issp_2007 <- subset(issp_2007, select=c("country",
                           "region_id_merging",
                           "V3",
                           "PARTY_LR",
                           "religiosity",
                           "chronotype_interval",
                           "day_off",
                           "urban_rural_area",
                           "age",
                           "degree",
                           "sex",
                           "political_interest",
                           "income",
                           "religious_denomination",
                           "latitude",
                           "longitude",
                           "mean_temperature",
                           "mid_field_season",
                           "CH_PRTY",
                           "FI_PRTY", 
                           "IE_PRTY",
                           "KR_PRTY", 
                           "MX_PRTY",
                           "NZ_PRTY",
                           "PH_PRTY", 
                           "RU_PRTY",
                           "nl_prty",
                           "sun_rise_local",
                           "solar_noon_local",
                           "sun_set_local",
                           "day_time_duration",
                           "ORIGINAL_MID_POINT"))

table(issp_2007$country, exclude = NULL)

issp_2007$voted_parties <- NA
issp_2007$voted_parties[issp_2007$country=="Greece"] <- "GR - Missing"
table(issp_2007$nl_prty, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==1] <- "NL - CDA"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==2] <- "NL - PvdA"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==3] <- "NL - VVD"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==4] <- "NL - SP"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==5] <- "NL - Groen Links"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==7] <- "NL - D66"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==8] <- "NL - Wilders-PVV"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==9] <- "NL - ChristenUnie"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==10] <- "NL - SGP"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==11] <- "NL - Partij van de Dieren"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==12] <- "NL - Lijst Verdonk Trots op Nederland"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==13] <- "NL - Other party"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==14] <- "NL - Would not vote"
issp_2007$voted_parties[issp_2007$country=="the Netherlands" & issp_2007$nl_prty==99] <- "NL - Missing"
table(issp_2007$CH_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==1] <- "CH - Christian Democratic Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==2] <- "CH - Evangelical Peoples Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==3] <- "CH - Radical Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==4] <- "CH - Social Democratic Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==5] <- "CH - Swiss Peoples Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==7] <- "CH - Liberal Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==8] <- "CH - Labour Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==9] <- "CH - Swiss Democrats"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==10] <- "CH - Green Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==11] <- "CH - Freedom Party"
issp_2007$voted_parties[issp_2007$country=="Switzerland" & issp_2007$CH_PRTY==95] <- "CH - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Switzerland" & is.na(issp_2007$voted_parties), "CH - Missing", issp_2007$voted_parties)
table(issp_2007$FI_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==1] <- "FI - Social Democr Party"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==2] <- "FI - Centre Party of FIN"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==3] <- "FI - Nat Coalition Party"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==4] <- "FI - Left Alliance"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==5] <- "FI - Swedish Peoples Prty"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==6] <- "FI - Green League"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==7] <- "FI - Christian League"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==8] <- "FI - True Finns"
issp_2007$voted_parties[issp_2007$country=="Finland" & issp_2007$FI_PRTY==95] <- "FI - Other Party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Finland" & is.na(issp_2007$voted_parties), "FI - Missing", issp_2007$voted_parties)
table(issp_2007$IE_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==1] <- "IE - Fianna Fail"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==2] <- "IE - Fine Gael"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==3] <- "IE - Labour"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==4] <- "IE - Progressive Democrats"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==5] <- "IE - Green Party"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==7] <- "IE - Sinn Fein"
issp_2007$voted_parties[issp_2007$country=="Ireland" & issp_2007$IE_PRTY==95] <- "IE - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Ireland" & is.na(issp_2007$voted_parties), "IE - Missing", issp_2007$voted_parties)
table(issp_2007$KR_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==1] <- "KR - People First Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==2] <- "KR - Democratic Labor Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==3] <- "KR - Uri Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==4] <- "KR - Democratic Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==5] <- "KR - Grand National Party"
issp_2007$voted_parties[issp_2007$country=="South Korea" & issp_2007$KR_PRTY==95] <- "KR - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="South Korea" & is.na(issp_2007$voted_parties), "KR - Missing", issp_2007$voted_parties)
table(issp_2007$MX_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==1] <- "MX - pan"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==2] <- "MX - pri"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==3] <- "MX - prd"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==4] <- "MX - pt"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==5] <- "MX - pvem"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==6] <- "MX - Convergencia"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==8] <- "MX - panal"
issp_2007$voted_parties[issp_2007$country=="Mexico" & issp_2007$MX_PRTY==95] <- "MX - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Mexico" & is.na(issp_2007$voted_parties), "MX - Missing", issp_2007$voted_parties)
table(issp_2007$NZ_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==1] <- "NZ - Act"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==2] <- "NZ - Alliance"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==3] <- "NZ - Green"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==4] <- "NZ - Labour"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==5] <- "NZ - National"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==6] <- "NZ - NZ First"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==7] <- "NZ - Progressive Coalition"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==8] <- "NZ - United Future"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==9] <- "NZ - Maori Party"
issp_2007$voted_parties[issp_2007$country=="New Zealand" & issp_2007$NZ_PRTY==95] <- "NZ - Other party"
issp_2007$voted_parties <- ifelse(issp_2007$country=="New Zealand" & is.na(issp_2007$voted_parties), "NZ - Missing", issp_2007$voted_parties)
table(issp_2007$PH_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==1] <- "PH - nationalista"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==3] <- "PH - OPOSISYON/OPPOSITION"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==4] <- "PH - LIBERAL PARTY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==5] <- "PH - kampi"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==6] <- "PH - independent"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==8] <- "PH - PARTIDO NINA TRILLANES AT ESCUDERO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==9] <- "PH - TEAM UNITY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==10] <- "PH - administration"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==13] <- "PH - PARTIDO NI ERAP"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==14] <- "PH - akbayan"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==15] <- "PH - FPJ PM - FERNANDO POE JR. - PARTIDONG MASA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==19] <- "PH - LAKAS-NUCD-CMD"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==24] <- "PH - buhay"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==28] <- "PH - cibac"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==29] <- "PH - MASANG PILIPINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==30] <- "PH - anakbayan"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==31] <- "PH - BAYAN MUNA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==33] <- "PH - laban"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==36] <- "PH - lakas"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==38] <- "PH - lacson"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==39] <- "PH - BOPK - BANDO OSME�A PUNDOK KAUSWAGAN"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==41] <- "PH - kababaihan"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==46] <- "PH - PARTIDO NI MAYOR CONSTANTINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==47] <- "PH - rightess"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==48] <- "PH - neutral"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==49] <- "PH - ANAK PAWIS"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==50] <- "PH - NPC-NATIONAL PEOPLE COALITION"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==51] <- "PH - OMPIA PARTY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==52] <- "PH - UMMAH PARTY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==53] <- "PH - PARTIDO NI CORY AQUINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==54] <- "PH - LABAN SA PARTIDO NI GLORIA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==55] <- "PH - LABAN NG DEMOKRATIKONG PILIPINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==56] <- "PH - PARTIDO NI JUN LOZADA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==57] <- "PH - PARTIDO NI SILVERIO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==58] <- "PH - amante"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==59] <- "PH - poverty"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==60] <- "PH - PARTIDO NI GOV.MIGS DOMINGUEZ (SARANGGANI)"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==61] <- "PH - PARTIDO NI MAYOR AGUILA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==62] <- "PH - DUTERTE PARA SA MASA"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==63] <- "PH - SAMAHANG WARAY"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==64] <- "PH - KILUSAN NG MASANG PILIPINO"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==65] <- "PH - CHRISTIAN MUSLIM DEMOCRATIC FEDERATION"
issp_2007$voted_parties[issp_2007$country=="the Philippines" & issp_2007$PH_PRTY==66] <- "PH - THE TEACHER"
issp_2007$voted_parties <- ifelse(issp_2007$country=="the Philippines" & is.na(issp_2007$voted_parties), "PH - Missing", issp_2007$voted_parties)
table(issp_2007$RU_PRTY, exclude = NULL)
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==1] <- "RU - Pensioners Party/Party of Social Justice"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==2] <- "RU - Union of right forces"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==3] <- "RU - Yabloko"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==4] <- "RU - Russian Ecological Party �Green� (Panfilov A.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==5] <- "RU - Country Party of Russia (Lapshin M.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==7] <- "RU - Party of Revival/Russian Party of Life (Seleznev G./ Miro"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==8] <- "RU - Motherland (Glazyev S.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==9] <- "RU - Liberal Democratic Party of Russia (Ghirinovsky V.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==10] <- "RU - United Russia (Gryzlov B.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==11] <- "RU - Communist Party of Russian Federation (Zyuganov G.)"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==95] <- "RU - Other party"
issp_2007$voted_parties[issp_2007$country=="Russia" & issp_2007$RU_PRTY==96] <- "RU - Against all/ threw out/ damaged voting paper"
issp_2007$voted_parties <- ifelse(issp_2007$country=="Russia" & is.na(issp_2007$voted_parties), "RU - Missing", issp_2007$voted_parties)
table(issp_2007$voted_parties, exclude = NULL)

table(issp_2007$religious_denomination, exclude = NULL)
issp_2007$religious_denomination <- ifelse(is.na(issp_2007$religious_denomination), "Missing", issp_2007$religious_denomination)

table(issp_2007$latitude, exclude = NULL)
issp_2007$latitude <- ifelse(is.na(issp_2007$latitude), -999999, issp_2007$latitude)

table(issp_2007$longitude, exclude = NULL)
issp_2007$longitude <- ifelse(is.na(issp_2007$longitude), -999999, issp_2007$longitude)

table(issp_2007$mean_temperature, exclude = NULL)
issp_2007$mean_temperature <- ifelse(is.na(issp_2007$mean_temperature), -999999, issp_2007$mean_temperature)

table(issp_2007$mid_field_season, exclude = NULL)
issp_2007$mid_field_season <- ifelse(is.na(issp_2007$mid_field_season), "Missing", issp_2007$mid_field_season)

issp_2007$latitude <- ifelse(issp_2007$latitude == -999999, NA, issp_2007$latitude)
issp_2007$longitude <- ifelse(issp_2007$longitude == -999999, NA, issp_2007$longitude)
issp_2007$mean_temperature <- ifelse(issp_2007$mean_temperature == -999999, NA, issp_2007$mean_temperature)
issp_2007$mid_field_season <- ifelse(issp_2007$mid_field_season == "Missing", NA, issp_2007$mid_field_season)

issp_2007 <- fastDummies::dummy_cols(issp_2007, select_columns = 'religious_denomination')

issp_2007$PARTY_LR_avg <- mean(issp_2007$PARTY_LR, na.rm = T)
issp_2007$PARTY_LR_sd <- sd(issp_2007$PARTY_LR, na.rm = T)
issp_2007$PARTY_LR_2sd <- issp_2007$PARTY_LR_sd*2
issp_2007$PARTY_LR_beta <- (issp_2007$PARTY_LR-issp_2007$PARTY_LR_avg)/issp_2007$PARTY_LR_2sd
table(issp_2007$PARTY_LR_beta, exclude = NULL)

issp_2007$religiosity_avg <- mean(issp_2007$religiosity, na.rm = T)
issp_2007$religiosity_sd <- sd(issp_2007$religiosity, na.rm = T)
issp_2007$religiosity_2sd <- issp_2007$religiosity_sd*2
issp_2007$religiosity_beta <- (issp_2007$religiosity-issp_2007$religiosity_avg)/issp_2007$religiosity_2sd
table(issp_2007$religiosity_beta, exclude = NULL)

issp_2007 <- issp_2007 %>%
  group_by(country) %>%
  mutate(group_mean_chronotype = mean(chronotype_interval, na.rm = T),
         group_sd_chronotype = sd(chronotype_interval, na.rm = T))

issp_2007$chronotype_interval_avg <- issp_2007$group_mean_chronotype
issp_2007$chronotype_interval_sd <- issp_2007$group_sd_chronotype
issp_2007$chronotype_interval_2sd <- issp_2007$chronotype_interval_sd*2
issp_2007$chronotype_interval_beta <- (issp_2007$chronotype_interval-issp_2007$chronotype_interval_avg)/issp_2007$chronotype_interval_2sd

issp_2007$day_off_avg <- mean(issp_2007$day_off, na.rm = T)
issp_2007$day_off_beta <- (issp_2007$day_off-issp_2007$day_off_avg)

table(issp_2007$urban_rural_area, exclude = NULL)
issp_2007$urban_rural_area_avg <- mean(issp_2007$urban_rural_area, na.rm = T)
issp_2007$urban_rural_area_sd <- sd(issp_2007$urban_rural_area, na.rm = T)
issp_2007$urban_rural_area_2sd <- issp_2007$urban_rural_area_sd*2
issp_2007$urban_rural_area_beta <- (issp_2007$urban_rural_area-issp_2007$urban_rural_area_avg)/issp_2007$urban_rural_area_2sd

table(issp_2007$age, exclude = NULL)
issp_2007$age_avg <- mean(issp_2007$age, na.rm = T)
issp_2007$age_sd <- sd(issp_2007$age, na.rm = T)
issp_2007$age_2sd <- issp_2007$age_sd*2
issp_2007$age_beta <- (issp_2007$age-issp_2007$age_avg)/issp_2007$age_2sd

table(issp_2007$degree, exclude = NULL)
issp_2007$degree_avg <- mean(issp_2007$degree, na.rm = T)
issp_2007$degree_sd <- sd(issp_2007$degree, na.rm = T)
issp_2007$degree_2sd <- issp_2007$degree_sd*2
issp_2007$degree_beta <- (issp_2007$degree-issp_2007$degree_avg)/issp_2007$degree_2sd

issp_2007$sex_dummy_avg <- mean(issp_2007$sex, na.rm = T)
issp_2007$sex_beta <- (issp_2007$sex-issp_2007$sex_dummy_avg)

issp_2007$political_interest_avg <- mean(issp_2007$political_interest, na.rm = T)
issp_2007$political_interest_sd <- sd(issp_2007$political_interest, na.rm = T)
issp_2007$political_interest_2sd <- issp_2007$political_interest_sd*2
issp_2007$political_interest_beta <- (issp_2007$political_interest-issp_2007$political_interest_avg)/issp_2007$political_interest_2sd

issp_2007$income_avg <- mean(issp_2007$income, na.rm = T)
issp_2007$income_sd <- sd(issp_2007$income, na.rm = T)
issp_2007$income_2sd <- issp_2007$income_sd*2
issp_2007$income_beta <- (issp_2007$income-issp_2007$income_avg)/issp_2007$income_2sd

table(issp_2007$religious_denomination_Buddhism, exclude = NULL)
issp_2007$religious_denomination_Buddhism_avg <- mean(issp_2007$religious_denomination_Buddhism, na.rm = T)
issp_2007$religious_denomination_Buddhism_beta <- (issp_2007$religious_denomination_Buddhism-issp_2007$religious_denomination_Buddhism_avg)
table(issp_2007$religious_denomination_Buddhism_beta, exclude = NULL)

table(issp_2007$religious_denomination_Christian_Orthodox, exclude = NULL)
issp_2007$religious_denomination_Christian_Orthodox_avg <- mean(issp_2007$religious_denomination_Christian_Orthodox, na.rm = T)
issp_2007$religious_denomination_Christian_Orthodox_beta <- (issp_2007$religious_denomination_Christian_Orthodox-issp_2007$religious_denomination_Christian_Orthodox_avg)
table(issp_2007$religious_denomination_Christian_Orthodox_beta, exclude = NULL)

table(issp_2007$religious_denomination_Hinduism, exclude = NULL)
issp_2007$religious_denomination_Hinduism_avg <- mean(issp_2007$religious_denomination_Hinduism, na.rm = T)
issp_2007$religious_denomination_Hinduism_beta <- (issp_2007$religious_denomination_Hinduism-issp_2007$religious_denomination_Hinduism_avg)
table(issp_2007$religious_denomination_Hinduism_beta, exclude = NULL)

table(issp_2007$religious_denomination_Islam, exclude = NULL)
issp_2007$religious_denomination_Islam_avg <- mean(issp_2007$religious_denomination_Islam, na.rm = T)
issp_2007$religious_denomination_Islam_beta <- (issp_2007$religious_denomination_Islam-issp_2007$religious_denomination_Islam_avg)
table(issp_2007$religious_denomination_Islam_beta, exclude = NULL)

table(issp_2007$religious_denomination_Jewish, exclude = NULL)
issp_2007$religious_denomination_Jewish_avg <- mean(issp_2007$religious_denomination_Jewish, na.rm = T)
issp_2007$religious_denomination_Jewish_beta <- (issp_2007$religious_denomination_Jewish-issp_2007$religious_denomination_Jewish_avg)
table(issp_2007$religious_denomination_Jewish_beta, exclude = NULL)

table(issp_2007$religious_denomination_No_religion, exclude = NULL)
issp_2007$religious_denomination_No_religion_avg <- mean(issp_2007$religious_denomination_No_religion, na.rm = T)
issp_2007$religious_denomination_No_religion_beta <- (issp_2007$religious_denomination_No_religion-issp_2007$religious_denomination_No_religion_avg)
table(issp_2007$religious_denomination_No_religion_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Christian_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Christian_Religions_avg <- mean(issp_2007$religious_denomination_Other_Christian_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Christian_Religions_beta <- (issp_2007$religious_denomination_Other_Christian_Religions-issp_2007$religious_denomination_Other_Christian_Religions_avg)
table(issp_2007$religious_denomination_Other_Christian_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Eastern_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Eastern_Religions_avg <- mean(issp_2007$religious_denomination_Other_Eastern_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Eastern_Religions_beta <- (issp_2007$religious_denomination_Other_Eastern_Religions-issp_2007$religious_denomination_Other_Eastern_Religions_avg)
table(issp_2007$religious_denomination_Other_Eastern_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Other_Religions, exclude = NULL)
issp_2007$religious_denomination_Other_Religions_avg <- mean(issp_2007$religious_denomination_Other_Religions, na.rm = T)
issp_2007$religious_denomination_Other_Religions_beta <- (issp_2007$religious_denomination_Other_Religions-issp_2007$religious_denomination_Other_Religions_avg)
table(issp_2007$religious_denomination_Other_Religions_beta, exclude = NULL)

table(issp_2007$religious_denomination_Protestant, exclude = NULL)
issp_2007$religious_denomination_Protestant_avg <- mean(issp_2007$religious_denomination_Protestant, na.rm = T)
issp_2007$religious_denomination_Protestant_beta <- (issp_2007$religious_denomination_Protestant-issp_2007$religious_denomination_Protestant_avg)
table(issp_2007$religious_denomination_Protestant_beta, exclude = NULL)

table(issp_2007$religious_denomination_Roman_Catholic, exclude = NULL)
issp_2007$religious_denomination_Roman_Catholic_avg <- mean(issp_2007$religious_denomination_Roman_Catholic, na.rm = T)
issp_2007$religious_denomination_Roman_Catholic_beta <- (issp_2007$religious_denomination_Roman_Catholic-issp_2007$religious_denomination_Roman_Catholic_avg)
table(issp_2007$religious_denomination_Roman_Catholic_beta, exclude = NULL)

#### Manuscript, Table 1. Descriptive Statistics ####

table1::table1(~ PARTY_LR + religiosity +
                 chronotype_interval +
         urban_rural_area +
         factor(sex) + age + degree + income + political_interest | country, data=issp_2007, digits=2)

#### Models ####

#### Left-Right Ideological Placement ####

issp_2007_left_right_model1 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model1)
tab_model(issp_2007_left_right_model1, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model1)*100,2)
lmerTest::ranova(issp_2007_left_right_model1)

issp_2007_left_right_model2 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model2)
tab_model(issp_2007_left_right_model2, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model2)*100,2)
lmerTest::ranova(issp_2007_left_right_model2)

library(optimx)
library(parallel)
library(minqa)

ncores <- detectCores()
diff_optims <- allFit(issp_2007_left_right_model2, maxfun = 1e5, parallel = 'multicore', ncpus = ncores)
is.OK <- sapply(diff_optims, is, "merMod")
diff_optims.OK <- diff_optims[is.OK]
lapply(diff_optims.OK,function(x) x@optinfo$conv$lme4$messages)

issp_2007_left_right_model2 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007,
                                    control = lmerControl(optimizer = "bobyqa"))
summary(issp_2007_left_right_model2)
tab_model(issp_2007_left_right_model2, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model2)*100,2)
lmerTest::ranova(issp_2007_left_right_model2)

issp_2007_left_right_model3 <- lmer(PARTY_LR_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                      sex_beta + age_beta + degree_beta + income_beta + religiosity_beta + political_interest_beta + (1 + chronotype_interval_beta | country), 
                                    data =  issp_2007)
summary(issp_2007_left_right_model3)
tab_model(issp_2007_left_right_model3, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_left_right_model3)*100,2)
lmerTest::ranova(issp_2007_left_right_model3)

#### Online Appendices, Table 20: Multilevel Linear Modeling of Left-Right Ideological Placement with Previously Excluded Sample ####


stargazer(issp_2007_left_right_model1, issp_2007_left_right_model2, issp_2007_left_right_model3,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_left_right_w_excluded_b16.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2", "Model 3"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Age", "Education", "Income",
                               "Religious Attendance", "Level on Interest in Politics",
                               "Intercept"))

tab_model(issp_2007_left_right_model1, show.ci = 0.95)
tab_model(issp_2007_left_right_model2, show.ci = 0.95)
tab_model(issp_2007_left_right_model3, show.ci = 0.95)

#### Preparation for Online Appendices, Table 21: Predicted Effects of Chronotype on Left-Right Ideological Placement for Each Country with Previously Excluded Sample ####

coef(issp_2007_left_right_model1)
arm::se.coef(issp_2007_left_right_model1)

coef(issp_2007_left_right_model1)$country
coef_issp_2007_left_right_model1 <- data.frame(coef(issp_2007_left_right_model1)$country)
coef_issp_2007_left_right_model1 <- subset(coef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model1$country <- row.names(coef_issp_2007_left_right_model1)
names(coef_issp_2007_left_right_model1)[names(coef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model1 <- data.frame(arm::se.coef(issp_2007_left_right_model1)$country)
se_coef_issp_2007_left_right_model1 <- subset(se_coef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model1$country <- row.names(se_coef_issp_2007_left_right_model1)
names(se_coef_issp_2007_left_right_model1)[names(se_coef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model1 <- merge(coef_issp_2007_left_right_model1, se_coef_issp_2007_left_right_model1, by="country")

coef_se_issp_2007_left_right_model1$chrono.cl.90 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cu.90 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cl.95 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cu.95 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cl.99 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model1$chrono.cu.99 <- coef_se_issp_2007_left_right_model1$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model1$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model1)

coef(issp_2007_left_right_model2)
arm::se.coef(issp_2007_left_right_model2)

coef(issp_2007_left_right_model2)$country
coef_issp_2007_left_right_model2 <- data.frame(coef(issp_2007_left_right_model2)$country)
coef_issp_2007_left_right_model2 <- subset(coef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model2$country <- row.names(coef_issp_2007_left_right_model2)
names(coef_issp_2007_left_right_model2)[names(coef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model2 <- data.frame(arm::se.coef(issp_2007_left_right_model2)$country)
se_coef_issp_2007_left_right_model2 <- subset(se_coef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model2$country <- row.names(se_coef_issp_2007_left_right_model2)
names(se_coef_issp_2007_left_right_model2)[names(se_coef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model2 <- merge(coef_issp_2007_left_right_model2, se_coef_issp_2007_left_right_model2, by="country")

coef_se_issp_2007_left_right_model2$chrono.cl.90 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cu.90 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cl.95 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cu.95 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cl.99 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model2$chrono.cu.99 <- coef_se_issp_2007_left_right_model2$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model2$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model2)

coef(issp_2007_left_right_model3)
arm::se.coef(issp_2007_left_right_model3)

coef(issp_2007_left_right_model3)$country
coef_issp_2007_left_right_model3 <- data.frame(coef(issp_2007_left_right_model3)$country)
coef_issp_2007_left_right_model3 <- subset(coef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
coef_issp_2007_left_right_model3$country <- row.names(coef_issp_2007_left_right_model3)
names(coef_issp_2007_left_right_model3)[names(coef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_left_right_model3 <- data.frame(arm::se.coef(issp_2007_left_right_model3)$country)
se_coef_issp_2007_left_right_model3 <- subset(se_coef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
se_coef_issp_2007_left_right_model3$country <- row.names(se_coef_issp_2007_left_right_model3)
names(se_coef_issp_2007_left_right_model3)[names(se_coef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_left_right_model3 <- merge(coef_issp_2007_left_right_model3, se_coef_issp_2007_left_right_model3, by="country")

coef_se_issp_2007_left_right_model3$chrono.cl.90 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cu.90 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cl.95 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cu.95 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cl.99 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
coef_se_issp_2007_left_right_model3$chrono.cu.99 <- coef_se_issp_2007_left_right_model3$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_left_right_model3$se_chronotype_interval_beta

View(coef_se_issp_2007_left_right_model3)

#### Preparation Online Appendices, Table 22: Random Effects of Chronotype on Left-Right Ideological Placement for Each Country with Previously Excluded Sample ####

ranef(issp_2007_left_right_model1)
arm::se.ranef(issp_2007_left_right_model1)

ranef(issp_2007_left_right_model1)$country
ranef_issp_2007_left_right_model1 <- data.frame(ranef(issp_2007_left_right_model1)$country)
ranef_issp_2007_left_right_model1 <- subset(ranef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model1$country <- row.names(ranef_issp_2007_left_right_model1)
names(ranef_issp_2007_left_right_model1)[names(ranef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model1 <- data.frame(arm::se.ranef(issp_2007_left_right_model1)$country)
se_ranef_issp_2007_left_right_model1 <- subset(se_ranef_issp_2007_left_right_model1, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model1$country <- row.names(se_ranef_issp_2007_left_right_model1)
names(se_ranef_issp_2007_left_right_model1)[names(se_ranef_issp_2007_left_right_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model1 <- merge(ranef_issp_2007_left_right_model1, se_ranef_issp_2007_left_right_model1, by="country")

ranef_se_issp_2007_left_right_model1$chrono.cl.90 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cu.90 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cl.95 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cu.95 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cl.99 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model1$chrono.cu.99 <- ranef_se_issp_2007_left_right_model1$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model1$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model1)

ranef(issp_2007_left_right_model2)
arm::se.ranef(issp_2007_left_right_model2)

ranef(issp_2007_left_right_model2)$country
ranef_issp_2007_left_right_model2 <- data.frame(ranef(issp_2007_left_right_model2)$country)
ranef_issp_2007_left_right_model2 <- subset(ranef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model2$country <- row.names(ranef_issp_2007_left_right_model2)
names(ranef_issp_2007_left_right_model2)[names(ranef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model2 <- data.frame(arm::se.ranef(issp_2007_left_right_model2)$country)
se_ranef_issp_2007_left_right_model2 <- subset(se_ranef_issp_2007_left_right_model2, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model2$country <- row.names(se_ranef_issp_2007_left_right_model2)
names(se_ranef_issp_2007_left_right_model2)[names(se_ranef_issp_2007_left_right_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model2 <- merge(ranef_issp_2007_left_right_model2, se_ranef_issp_2007_left_right_model2, by="country")

ranef_se_issp_2007_left_right_model2$chrono.cl.90 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cu.90 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cl.95 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cu.95 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cl.99 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model2$chrono.cu.99 <- ranef_se_issp_2007_left_right_model2$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model2$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model2)

ranef(issp_2007_left_right_model3)
arm::se.ranef(issp_2007_left_right_model3)

ranef(issp_2007_left_right_model3)$country
ranef_issp_2007_left_right_model3 <- data.frame(ranef(issp_2007_left_right_model3)$country)
ranef_issp_2007_left_right_model3 <- subset(ranef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
ranef_issp_2007_left_right_model3$country <- row.names(ranef_issp_2007_left_right_model3)
names(ranef_issp_2007_left_right_model3)[names(ranef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_left_right_model3 <- data.frame(arm::se.ranef(issp_2007_left_right_model3)$country)
se_ranef_issp_2007_left_right_model3 <- subset(se_ranef_issp_2007_left_right_model3, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_left_right_model3$country <- row.names(se_ranef_issp_2007_left_right_model3)
names(se_ranef_issp_2007_left_right_model3)[names(se_ranef_issp_2007_left_right_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_left_right_model3 <- merge(ranef_issp_2007_left_right_model3, se_ranef_issp_2007_left_right_model3, by="country")

ranef_se_issp_2007_left_right_model3$chrono.cl.90 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cu.90 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cl.95 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cu.95 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cl.99 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta
ranef_se_issp_2007_left_right_model3$chrono.cu.99 <- ranef_se_issp_2007_left_right_model3$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_left_right_model3$se_chronotype_interval_beta

View(ranef_se_issp_2007_left_right_model3)

#### Religious Attendance ####

issp_2007_religiosity_model1 <- lmer(religiosity_beta ~ chronotype_interval_beta + (1 + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model1)
tab_model(issp_2007_religiosity_model1, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model1)*100,2)
lmerTest::ranova(issp_2007_religiosity_model1)

issp_2007_religiosity_model2 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta + (1 + chronotype_interval_beta  | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model2)
tab_model(issp_2007_religiosity_model2, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model2)*100,2)
lmerTest::ranova(issp_2007_religiosity_model2)

issp_2007_religiosity_model3 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                       sex_beta + age_beta + degree_beta + income_beta + political_interest_beta + (1  + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model3)
tab_model(issp_2007_religiosity_model3, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model3)*100,2)
lmerTest::ranova(issp_2007_religiosity_model3)

issp_2007_religiosity_model4 <- lmer(religiosity_beta ~ chronotype_interval_beta + urban_rural_area_beta +
                                       sex_beta + age_beta + degree_beta + income_beta + political_interest_beta + PARTY_LR_beta + (1  + chronotype_interval_beta | country), 
                                     data =  issp_2007)
summary(issp_2007_religiosity_model4)
tab_model(issp_2007_religiosity_model4, show.ci = 0.95)
round(variancePartition::calcVarPart(issp_2007_religiosity_model4)*100,2)
lmerTest::ranova(issp_2007_religiosity_model4)

#### Online Appendices, Table 23: Multilevel Linear Modeling of Religious Attendance with Previously Excluded Sample ####

stargazer(issp_2007_religiosity_model1, issp_2007_religiosity_model2, issp_2007_religiosity_model3, issp_2007_religiosity_model4,
          type = "html", title=" ", digits=2, out="Tables/models_issp_2007_religiosity_w_excluded.htm",
          model.numbers = F,
          column.labels = c("Model 1", "Model 2", "Model 3", "Model 4"),
          covariate.labels = c("Chronotype", 
                               "Urban-Rural Area of Residence", "Sex: Male (Base: Female)",
                               "Age", "Education", "Income",
                               "Level on Interest in Politics", "Left-Right Ideological Placement", 
                               "Intercept"))

tab_model(issp_2007_religiosity_model1, show.ci = 0.95)
tab_model(issp_2007_religiosity_model2, show.ci = 0.95)
tab_model(issp_2007_religiosity_model3, show.ci = 0.95)
tab_model(issp_2007_religiosity_model4, show.ci = 0.95)

#### Preparation for Online Appendices, Table 24: Predicted Effects of Chronotype on Religious Attendance for Each Country with Previously Excluded Sample ####

coef(issp_2007_religiosity_model1)
arm::se.coef(issp_2007_religiosity_model1)

coef(issp_2007_religiosity_model1)$country
coef_issp_2007_religiosity_model1 <- data.frame(coef(issp_2007_religiosity_model1)$country)
coef_issp_2007_religiosity_model1 <- subset(coef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model1$country <- row.names(coef_issp_2007_religiosity_model1)
names(coef_issp_2007_religiosity_model1)[names(coef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model1 <- data.frame(arm::se.coef(issp_2007_religiosity_model1)$country)
se_coef_issp_2007_religiosity_model1 <- subset(se_coef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model1$country <- row.names(se_coef_issp_2007_religiosity_model1)
names(se_coef_issp_2007_religiosity_model1)[names(se_coef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model1 <- merge(coef_issp_2007_religiosity_model1, se_coef_issp_2007_religiosity_model1, by="country")

coef_se_issp_2007_religiosity_model1$chrono.cl.90 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cu.90 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cl.95 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cu.95 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cl.99 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model1$chrono.cu.99 <- coef_se_issp_2007_religiosity_model1$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model1)

coef(issp_2007_religiosity_model2)
arm::se.coef(issp_2007_religiosity_model2)

coef(issp_2007_religiosity_model2)$country
coef_issp_2007_religiosity_model2 <- data.frame(coef(issp_2007_religiosity_model2)$country)
coef_issp_2007_religiosity_model2 <- subset(coef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model2$country <- row.names(coef_issp_2007_religiosity_model2)
names(coef_issp_2007_religiosity_model2)[names(coef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model2 <- data.frame(arm::se.coef(issp_2007_religiosity_model2)$country)
se_coef_issp_2007_religiosity_model2 <- subset(se_coef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model2$country <- row.names(se_coef_issp_2007_religiosity_model2)
names(se_coef_issp_2007_religiosity_model2)[names(se_coef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model2 <- merge(coef_issp_2007_religiosity_model2, se_coef_issp_2007_religiosity_model2, by="country")

coef_se_issp_2007_religiosity_model2$chrono.cl.90 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cu.90 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cl.95 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cu.95 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cl.99 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model2$chrono.cu.99 <- coef_se_issp_2007_religiosity_model2$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model2)

coef(issp_2007_religiosity_model3)
arm::se.coef(issp_2007_religiosity_model3)

coef(issp_2007_religiosity_model3)$country
coef_issp_2007_religiosity_model3 <- data.frame(coef(issp_2007_religiosity_model3)$country)
coef_issp_2007_religiosity_model3 <- subset(coef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model3$country <- row.names(coef_issp_2007_religiosity_model3)
names(coef_issp_2007_religiosity_model3)[names(coef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model3 <- data.frame(arm::se.coef(issp_2007_religiosity_model3)$country)
se_coef_issp_2007_religiosity_model3 <- subset(se_coef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model3$country <- row.names(se_coef_issp_2007_religiosity_model3)
names(se_coef_issp_2007_religiosity_model3)[names(se_coef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model3 <- merge(coef_issp_2007_religiosity_model3, se_coef_issp_2007_religiosity_model3, by="country")

coef_se_issp_2007_religiosity_model3$chrono.cl.90 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cu.90 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cl.95 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cu.95 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cl.99 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model3$chrono.cu.99 <- coef_se_issp_2007_religiosity_model3$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model3)

coef(issp_2007_religiosity_model4)
arm::se.coef(issp_2007_religiosity_model4)

coef(issp_2007_religiosity_model4)$country
coef_issp_2007_religiosity_model4 <- data.frame(coef(issp_2007_religiosity_model4)$country)
coef_issp_2007_religiosity_model4 <- subset(coef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
coef_issp_2007_religiosity_model4$country <- row.names(coef_issp_2007_religiosity_model4)
names(coef_issp_2007_religiosity_model4)[names(coef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "coef_chronotype_interval_beta"


se_coef_issp_2007_religiosity_model4 <- data.frame(arm::se.coef(issp_2007_religiosity_model4)$country)
se_coef_issp_2007_religiosity_model4 <- subset(se_coef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
se_coef_issp_2007_religiosity_model4$country <- row.names(se_coef_issp_2007_religiosity_model4)
names(se_coef_issp_2007_religiosity_model4)[names(se_coef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

coef_se_issp_2007_religiosity_model4 <- merge(coef_issp_2007_religiosity_model4, se_coef_issp_2007_religiosity_model4, by="country")

coef_se_issp_2007_religiosity_model4$chrono.cl.90 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta - 1.645*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cu.90 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta + 1.645*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cl.95 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta - 1.96*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cu.95 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta + 1.96*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cl.99 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta - 2.576*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
coef_se_issp_2007_religiosity_model4$chrono.cu.99 <- coef_se_issp_2007_religiosity_model4$coef_chronotype_interval_beta + 2.576*coef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta

View(coef_se_issp_2007_religiosity_model4)

#### Preparation Online Appendices, Table 25: Random Effects of Chronotype on Religious Attendance for Each Country with Previously Excluded Sample ####

ranef(issp_2007_religiosity_model1)
arm::se.ranef(issp_2007_religiosity_model1)

ranef(issp_2007_religiosity_model1)$country
ranef_issp_2007_religiosity_model1 <- data.frame(ranef(issp_2007_religiosity_model1)$country)
ranef_issp_2007_religiosity_model1 <- subset(ranef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model1$country <- row.names(ranef_issp_2007_religiosity_model1)
names(ranef_issp_2007_religiosity_model1)[names(ranef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model1 <- data.frame(arm::se.ranef(issp_2007_religiosity_model1)$country)
se_ranef_issp_2007_religiosity_model1 <- subset(se_ranef_issp_2007_religiosity_model1, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model1$country <- row.names(se_ranef_issp_2007_religiosity_model1)
names(se_ranef_issp_2007_religiosity_model1)[names(se_ranef_issp_2007_religiosity_model1)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model1 <- merge(ranef_issp_2007_religiosity_model1, se_ranef_issp_2007_religiosity_model1, by="country")

ranef_se_issp_2007_religiosity_model1$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model1$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model1$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model1$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model1)

ranef(issp_2007_religiosity_model2)
arm::se.ranef(issp_2007_religiosity_model2)

ranef(issp_2007_religiosity_model2)$country
ranef_issp_2007_religiosity_model2 <- data.frame(ranef(issp_2007_religiosity_model2)$country)
ranef_issp_2007_religiosity_model2 <- subset(ranef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model2$country <- row.names(ranef_issp_2007_religiosity_model2)
names(ranef_issp_2007_religiosity_model2)[names(ranef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model2 <- data.frame(arm::se.ranef(issp_2007_religiosity_model2)$country)
se_ranef_issp_2007_religiosity_model2 <- subset(se_ranef_issp_2007_religiosity_model2, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model2$country <- row.names(se_ranef_issp_2007_religiosity_model2)
names(se_ranef_issp_2007_religiosity_model2)[names(se_ranef_issp_2007_religiosity_model2)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model2 <- merge(ranef_issp_2007_religiosity_model2, se_ranef_issp_2007_religiosity_model2, by="country")

ranef_se_issp_2007_religiosity_model2$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model2$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model2$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model2$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model2)

ranef(issp_2007_religiosity_model3)
arm::se.ranef(issp_2007_religiosity_model3)

ranef(issp_2007_religiosity_model3)$country
ranef_issp_2007_religiosity_model3 <- data.frame(ranef(issp_2007_religiosity_model3)$country)
ranef_issp_2007_religiosity_model3 <- subset(ranef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model3$country <- row.names(ranef_issp_2007_religiosity_model3)
names(ranef_issp_2007_religiosity_model3)[names(ranef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model3 <- data.frame(arm::se.ranef(issp_2007_religiosity_model3)$country)
se_ranef_issp_2007_religiosity_model3 <- subset(se_ranef_issp_2007_religiosity_model3, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model3$country <- row.names(se_ranef_issp_2007_religiosity_model3)
names(se_ranef_issp_2007_religiosity_model3)[names(se_ranef_issp_2007_religiosity_model3)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model3 <- merge(ranef_issp_2007_religiosity_model3, se_ranef_issp_2007_religiosity_model3, by="country")

ranef_se_issp_2007_religiosity_model3$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model3$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model3$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model3$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model3)

ranef(issp_2007_religiosity_model4)
arm::se.ranef(issp_2007_religiosity_model4)

ranef(issp_2007_religiosity_model4)$country
ranef_issp_2007_religiosity_model4 <- data.frame(ranef(issp_2007_religiosity_model4)$country)
ranef_issp_2007_religiosity_model4 <- subset(ranef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
ranef_issp_2007_religiosity_model4$country <- row.names(ranef_issp_2007_religiosity_model4)
names(ranef_issp_2007_religiosity_model4)[names(ranef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "ranef_chronotype_interval_beta"


se_ranef_issp_2007_religiosity_model4 <- data.frame(arm::se.ranef(issp_2007_religiosity_model4)$country)
se_ranef_issp_2007_religiosity_model4 <- subset(se_ranef_issp_2007_religiosity_model4, select = c("chronotype_interval_beta"))
se_ranef_issp_2007_religiosity_model4$country <- row.names(se_ranef_issp_2007_religiosity_model4)
names(se_ranef_issp_2007_religiosity_model4)[names(se_ranef_issp_2007_religiosity_model4)=="chronotype_interval_beta"] <- "se_chronotype_interval_beta"

ranef_se_issp_2007_religiosity_model4 <- merge(ranef_issp_2007_religiosity_model4, se_ranef_issp_2007_religiosity_model4, by="country")

ranef_se_issp_2007_religiosity_model4$chrono.cl.90 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta - 1.645*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cu.90 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta + 1.645*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cl.95 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta - 1.96*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cu.95 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta + 1.96*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cl.99 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta - 2.576*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta
ranef_se_issp_2007_religiosity_model4$chrono.cu.99 <- ranef_se_issp_2007_religiosity_model4$ranef_chronotype_interval_beta + 2.576*ranef_se_issp_2007_religiosity_model4$se_chronotype_interval_beta

View(ranef_se_issp_2007_religiosity_model4)
